using System;
using Castle.ActiveRecord;
using KenEgozi.Com.Domain.Aggregates.Post;
using NUnit.Framework;
using Castle.ActiveRecord.Framework;
using System.Configuration;
using System.Diagnostics;
using Rhino.Commons;


namespace KenEgozi.Com.Domain.Tests
{
    public abstract class AbstractModelTestCase
    {
        protected delegate void RollbackingTestDelegate();

        protected static void ExecuteInTransaction(RollbackingTestDelegate d)
        {
			UnitOfWork.Start();
			With.Transaction(delegate 
         	{
                try
                {
                    d();
                }
                catch (Exception ex)
                {
                    Assert.Fail(ex.ToString());
                }
				UnitOfWork.CurrentSession.Transaction.Rollback();
         	});
        }

        [TestFixtureSetUp]
        public virtual void FixtureInit()
        {
            try
            {
                InitFramework();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
                throw;
            }
        }

        [SetUp]
        public virtual void Init()
        {
			UnitOfWork.Start();
        }

        [TearDown]
        public virtual void Terminate()
        {
			UnitOfWork.Current.Dispose();
        }

        [TestFixtureTearDown]
        public virtual void TerminateAll()
        {
        }

        protected virtual void InitFramework()
        {
			IoCContainer container = new IoCContainer();
			container.Initialize();
			IoC.Initialize(container);

            IConfigurationSource source = ConfigurationManager.GetSection("activerecord") as IConfigurationSource;

            ActiveRecordStarter.ResetInitializationFlag();
            ActiveRecordStarter.Initialize(typeof(Post).Assembly, source);
        }
    }
}
